package com.kin207.netty;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;
import com.kin207.netty.handle.IMessageHandle;
import com.kin207.netty.handle.MessageHandles;
import com.kin207.netty.handle.adapter.HeadMessageHandleAdapter;
import com.kin207.netty.handle.adapter.HexHeadMessageHandleAdapter;
import com.kin207.netty.listener.Listener;
import com.kin207.netty.listener.ListenerConfig;
import com.kin207.netty.listener.eventbus.EventBusListener;
import com.kin207.netty.listener.server.InstanceServerListener;
import com.kin207.netty.msg.parse.SplitMessageParse;
import com.kin207.netty.session.SessionManager;
import com.kin207.netty.tcp.TcpChannelHandlerInitializer;
import com.kin207.netty.tcp.TcpChannelInboundHandler;
import com.kin207.netty.tcp.TcpChannelInitializer;
import com.kin207.netty.tcp.TcpServer;

import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class TcpServerRun {
	final static Logger logger = LoggerFactory.getLogger(TcpServerRun.class );
	
	public static void main(String[] args) {
		ListenerConfig listenerConfig = new ListenerConfig();
		listenerConfig.setParams(new HashMap(){{
			put("messageHandles", "com.kin207.netty.listener.server.handle.ServerListMessageHandle,com.kin207.netty.listener.server.handle.ServerCloseMessageHandle");
		}});
		
		
		InstanceServerListener instanceServerListener = new InstanceServerListener();
		instanceServerListener.initialize(listenerConfig);
		
		EventBusListener<Listener> eventBusListener = new EventBusListener();
		eventBusListener.addListener(instanceServerListener);
		
		EventBus serverEventBus = new EventBus();
		serverEventBus.register(eventBusListener);
		
		//TcpSessionManager sessionManager = new TcpSessionManager();
		//SessionManager sessionManager = new SessionManager();
		SessionManager sessionManager = SessionManager.newInstance();
		
		final int port = args.length>0?Integer.parseInt(args[0]):8379;
		Map<String,IMessageHandle> handles = MessageHandles.newInstance().getHandles();
		final TcpServer server = new TcpServer(port);
		//HeadMessageHandleAdapter messageHandleAdapter = new HeadMessageHandleAdapter();
		HexHeadMessageHandleAdapter messageHandleAdapter = new HexHeadMessageHandleAdapter();
		messageHandleAdapter.setHandles(handles);
		
		SplitMessageParse messageParse = new SplitMessageParse();
		
		TcpChannelInboundHandler channelInboundHandler = new TcpChannelInboundHandler();
		channelInboundHandler.setServer(server);
		
		Map<String, ChannelHandler> channelHandlers = new LinkedHashMap();
		//channelHandlers.put("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
		channelHandlers.put("decoder", new StringDecoder());
		channelHandlers.put("encoder", new StringEncoder());
		channelHandlers.put("handler", channelInboundHandler);
		
		TcpChannelInitializer channelInitializer = new TcpChannelInitializer();
		channelInitializer.setInitHandles(channelHandlers);
		channelInitializer.setChannelHandlerInitializer(new TcpChannelHandlerInitializer());
		
		server.setSessionManager(sessionManager);
		server.setChannelInitializer(channelInitializer);
		server.setMessageHandleAdapter(messageHandleAdapter);
		server.setMessageParse(messageParse);
		server.setEventBus(serverEventBus);
		
		Executors.newSingleThreadExecutor().submit(new Runnable() {
			public void run() {
				logger.info("tcp server run, prot:{}", port);
				try {
					server.run();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		});
		
		if(args.length<1 || "in".equals(args[1])){
			Scanner scann = new Scanner(System.in);
			while(true){
				System.out.print("请输入:");
				String msg = scann.nextLine();
				server.getSessionManager().sendSystemMessage(msg,System.currentTimeMillis());
			}
		}
	}
}
